package com.gyt.manager.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.gyt.manager.common.base.exception.BaseException;
import com.gyt.manager.common.base.response.Page;
import com.gyt.manager.common.base.response.Response;
import com.gyt.manager.common.utils.DateTimeUtility;
import com.gyt.manager.dao.risk.TaskInfoOfflineMapper;
import com.gyt.manager.dao.risk.TaskWhiteInfoMapper;
import com.gyt.manager.domain.req.TaskWhiteDaysReq;
import com.gyt.manager.domain.req.TaskWhiteReq;
import com.gyt.manager.domain.risk.TaskInfoOffline;
import com.gyt.manager.domain.risk.TaskWhiteInfo;
import com.gyt.manager.service.TaskService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author gyt on 2021/4/28 12:48
 */
@Service
public class TaskServiceImpl implements TaskService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TaskServiceImpl.class);

    @Autowired
    private TaskInfoOfflineMapper taskInfoOfflineMapper;
    @Autowired
    private TaskWhiteInfoMapper taskWhiteInfoMapper;

    @Override
    public Boolean batchAddTaskWhite(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new BaseException(Response.CODE_ERROR, "ids不能为空");
        }
        List<TaskInfoOffline> taskOfflines = taskInfoOfflineMapper.selectByIds(ids);
        int keepNum = 30;
        Date date = new Date();
        List<TaskWhiteInfo> whiteInfos = Lists.newArrayList();
        for (TaskInfoOffline taskOffline : taskOfflines) {
            TaskWhiteInfo whiteInfo =
                    getTaskWhiteInfo(date, taskOffline, keepNum);
            whiteInfos.add(whiteInfo);
        }
        return taskWhiteInfoMapper.batchInsert(whiteInfos);
    }

    @Override
    public Boolean batchUpdateTaskWhiteDays(TaskWhiteDaysReq req) {
        Integer fkeepNum = req.getFkeepNum();
        if (fkeepNum < 0) {
            throw new BaseException(Response.CODE_ERROR, "fkeepNum传参错误");
        }
        List<Long> ids = req.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            throw new BaseException(Response.CODE_ERROR, "ids不能为空");
        }
        List<TaskWhiteInfo> whiteInfos = taskWhiteInfoMapper.selectByIds(ids);
        Date date = new Date();
        for (TaskWhiteInfo whiteInfo : whiteInfos) {
            Date startDate = whiteInfo.getFstartDate();
            Date endDate = DateTimeUtility.addDays(startDate, fkeepNum);
            TaskWhiteInfo update = new TaskWhiteInfo();
            update.setFid(whiteInfo.getFid());
            update.setFkeepNum(fkeepNum);
            update.setFendDate(endDate);
            update.setFmodifyTime(date);
            taskWhiteInfoMapper.updateTaskWhite(update);
        }
        return true;
    }

    @Override
    public Page<TaskWhiteInfo> findTaskInfos(TaskWhiteReq taskWhiteReq) {
        List<TaskWhiteInfo> list = Lists.newArrayList();
        Integer whiteType = taskWhiteReq.getWhiteType();
        if (whiteType == 0) {
            list = getAllTaskWhiteInfos(taskWhiteReq);
        } else if (whiteType == 1) {
            list = getTaskWhiteInfos(taskWhiteReq);
        }
        Page<TaskWhiteInfo> from = Page.from(list);
        return from;
    }

    private List<TaskWhiteInfo> getAllTaskWhiteInfos(TaskWhiteReq taskWhiteReq) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("keyword", taskWhiteReq.getKeyword());
        Page.startPage(taskWhiteReq.getPageNum(), taskWhiteReq.getPageSize());
        List<TaskWhiteInfo> list = taskWhiteInfoMapper.selectAllByCondition(paramMap);
        list.forEach(e -> {
            if (e.getFisWhite() == null) {
                e.setFisWhite((byte) 0);
            }
        });
        return list;
    }

    private List<TaskWhiteInfo> getTaskWhiteInfos(TaskWhiteReq taskWhiteReq) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("fowners", taskWhiteReq.getOwners());
        paramMap.put("keyword", taskWhiteReq.getKeyword());
        paramMap.put("orderKey", taskWhiteReq.getOrderKey());
        paramMap.put("sortType", taskWhiteReq.getSortType());
        Page.startPage(taskWhiteReq.getPageNum(), taskWhiteReq.getPageSize());
        List<TaskWhiteInfo> list = taskWhiteInfoMapper.selectByCondition(paramMap);
        return list;
    }

    private TaskWhiteInfo getTaskWhiteInfo(Date date,
                                           TaskInfoOffline taskOffline, Integer keepNum) {
        TaskWhiteInfo whiteInfo = new TaskWhiteInfo();
        whiteInfo.setFbusinessName(taskOffline.getFbusinessName());
        whiteInfo.setFbusinessType(taskOffline.getFbusinessType());
        whiteInfo.setFcreateTime(date);
        whiteInfo.setFcycleNum(taskOffline.getFcycleNum());
        whiteInfo.setFendDate(date);
        whiteInfo.setFisWhite(NumberUtils.BYTE_ONE);
        whiteInfo.setFkeepNum(keepNum);
        whiteInfo.setFmodifyTime(date);
        whiteInfo.setFowners(taskOffline.getFowners());
        whiteInfo.setFstartDate(date);
        whiteInfo.setFstatus(taskOffline.getFstatus());
        whiteInfo.setFtaskBaseId(taskOffline.getFtaskBaseId());
        whiteInfo.setFtaskBaseName(taskOffline.getFtaskBaseName());
        whiteInfo.setFtaskId(taskOffline.getFtaskId());
        whiteInfo.setFtaskName(taskOffline.getFtaskName());
        return whiteInfo;
    }
}
